package com.yiren.manager.ctrl.test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrTestTopic;
import com.yiren.entity.YrTestTopicItem;
import com.yiren.entity.YrTestTopicItemAnswer;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.test.YrTestTopicForm;
import com.yiren.manager.form.test.YrTestTopicItemAnswerForm;
import com.yiren.manager.form.test.YrTestTopicItemForm;
import com.yiren.manager.service.iface.test.YrTestTopicItemAnswerService;
import com.yiren.manager.service.iface.test.YrTestTopicItemService;
import com.yiren.manager.service.iface.test.YrTestTopicService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("testTopic")
@Controller
public class YrTestTopicController extends GenericController<YrTestTopicForm, YrTestTopic>{

	@Autowired
	private YrTestTopicService selfService;
	
	@Autowired
	private YrTestTopicItemService testTopicItemService;
	
	@Autowired
	private YrTestTopicItemAnswerService testTopicItemAnswerService;
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrTestTopicForm> find(Integer searchVal) throws Exception{
		if(searchVal==null){
			throwExp("参数有误");
		}
		HTTPResult<YrTestTopicForm> result = new HTTPResult<YrTestTopicForm>();
		String sqlWhere = "topic_code_num=?1";
		QueryResult<YrTestTopic> qr = selfService.find(YrTestTopic.class, sqlWhere, new Object[]{searchVal});
		if(qr.getTotalRecord()==0){
			throwExp("查不到相关题目");
		}
		YrTestTopicForm form = toForm(qr.getResultList().get(0));
		sqlWhere = "test_topic_id=?1";
		QueryResult<YrTestTopicItem> qrTestTopicItem = testTopicItemService.find(YrTestTopicItem.class, sqlWhere, new Object[]{form.getTestTopicId()});
		List<YrTestTopicItemForm> testTopicItemForms = new ArrayList<YrTestTopicItemForm>();
		for(YrTestTopicItem topicItem : qrTestTopicItem.getResultList()){
			YrTestTopicItemForm testTopicItemForm = ObjectUtils.objectToObject(topicItem, YrTestTopicItemForm.class);
			sqlWhere = "test_topic_item_id=?1";
			QueryResult<YrTestTopicItemAnswer> qrTestTopicItemAnswer = testTopicItemAnswerService.find(YrTestTopicItemAnswer.class, sqlWhere, new Object[]{topicItem.getTestTopicItemId()});
			List<YrTestTopicItemAnswerForm> testTopicItemAnswerForms = new ArrayList<YrTestTopicItemAnswerForm>();
			for(YrTestTopicItemAnswer testTopicItemAnswer : qrTestTopicItemAnswer.getResultList()){
				testTopicItemAnswerForms.add(ObjectUtils.objectToObject(testTopicItemAnswer, YrTestTopicItemAnswerForm.class));
			}
			testTopicItemForm.setTestTopicItemAnswerForms(testTopicItemAnswerForms);
			testTopicItemForms.add(testTopicItemForm);
		}
		form.setTestTopicItemForms(testTopicItemForms);
		
		List<YrTestTopicForm> formList = new ArrayList<YrTestTopicForm>();
		formList.add(form);
		result.setData(formList);
		
		return result;
	}
	
	@Override
	public YrTestTopicForm toForm(YrTestTopic e) throws Exception {
		YrTestTopicForm form = ObjectUtils.objectToObject(e, YrTestTopicForm.class);
		return form;
	}

	@Override
	public YrTestTopic toEntity(YrTestTopicForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
